﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Epic.Net.Extensions
{
    public static class StringExtensions
    {
        public static bool IsSame(this string value, string compare, bool ignoreCase = true)
        {
            return String.Compare(value, compare, ignoreCase) == 0;
        }




        public static T Enum<T>(this string value, bool ignoreCase = true) where T : struct
        {
            T result;
            System.Enum.TryParse<T>(value, ignoreCase, out result);
            return result;
        }

        public static bool Boolean(this string value, bool defaultValue = false)
        {
            if (String.IsNullOrWhiteSpace(value)) return defaultValue;
            if (value.Length == 1)
            {
                if (value[0] == '1') return true;
                if (value[0] == '0') return false;
            }

            if (value.Length == 4 && value.IsSame("true")) return true;
            if (value.Length == 5 && value.IsSame("false")) return false;

            return System.Boolean.Parse(value);
        }
    }

}